<?php
include_once 'database/db.php';
@$fileId=$_GET['id'];
if ($fileId) {
  // 1. 在mongodb数据库查询相应文件信息
  $DB = new DB();
  $cursor = $DB->query('fileshare.files',['id' => $fileId]);

  $body = "";
  $count=0;
  foreach($cursor as $document) {
    $count++;
    $fileName = $document->fileName;
    $totalBlobNum = $document->totalBlobNum;
    $lastBlobSize = 15 * 1024 * 1024;
    $blobSize = $document->blobSize;
    for($i=1;$i<=$totalBlobNum;$i++){
      if ($i==$totalBlobNum) {
        $lastBlobSize = $document->lastBlobSize;
        $line = "-".' '.$lastBlobSize.' '."/fileshare/".$fileId.'/'.$fileName."__".$i.' '.$fileName."__".$i."\n";
        $body = $body.$line;
      }else {
        $line = "-".' '.$blobSize.' '."/fileshare/".$fileId.'/'.$fileName."__".$i.' '.$fileName."__".$i."\n";
        $body = $body.$line;
      }
    }
  }

  if ($count!=1) {
    // 未查询到相应文件信息
    echo "未查询到相应文件信息";
  }else {
    header('X-Archive-Files: zip');
    echo $body; // 发送响应体
  }

}else {
  echo "非法请求!";
}
